package com.accounting.client.frontend.cost.dialog;

import com.accounting.client.remote.Services;
import com.accounting.client.remote.enumeration.CostStatus;
import com.accounting.client.remote.enumeration.Costs;
import com.accounting.client.remote.enumeration.Currency;
import com.accounting.client.remote.transfer.AddCostRequest;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.delegate.Action;
import corelib.client.resource.CommonResources;
import corelib.client.ui.widget.Size;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.WindowHandle;
import corelib.client.ui.widget.button.IsButton;
import corelib.client.ui.widget.dialog.SimpleDialogHandler;
import corelib.client.ui.widget.form.DecimalBox;
import corelib.client.ui.widget.form.SelectField;
import corelib.client.ui.widget.form.SimpleLabel;
import corelib.client.ui.widget.form.TextAreaField;
import corelib.client.ui.widget.form.fomatters.ValueFormatter;
import corelib.client.ui.widget.layout.LabelAlignment;
import corelib.client.ui.widget.layout.RelativeColumn;
import corelib.client.ui.widget.layout.RelativeLayout;

public class AddCostDialog extends SimpleDialogHandler {
	
	private SelectField<Integer> typeField;
	private SelectField<CostStatus> statusField;
	private TextAreaField descriptionField;
	private SelectField<Currency> currencyField;
	private DecimalBox amountField;
	
	private Action action;
	
	public AddCostDialog(Action action){
		this.action = action;
	}
	
	@Override
	public String getHeading() {
		return "创建新的花费";
	}

	@Override
	public Size getSize() {
		return new Size(550, 400);
	}

	@Override
	public Widget getContentPanel() {
		amountField = new DecimalBox();
		descriptionField = new TextAreaField();
		descriptionField.setHeight(150);
		
		typeField = new SelectField<Integer>(Costs.AllCosts);
		typeField.setNullable(true);
		typeField.setOptionFormatter("请选择花费类型", new ValueFormatter<Integer>(){
			@Override
			public String format(Integer value) {
				return Costs.GetCName(value);
			}
		});
		
		statusField = new SelectField<CostStatus>(CostStatus.All);
		statusField.setNullable(true);
		statusField.setOptionFormatter("请选择状态", new ValueFormatter<CostStatus>(){
			@Override
			public String format(CostStatus value) {
				return value.getName();
			}
		});
		
		currencyField = new SelectField<Currency>(Currency.All);
		currencyField.setNullable(true);
		currencyField.setOptionFormatter("请选择货币类型",new ValueFormatter<Currency>(){
			@Override
			public String format(Currency value) {
				return value.getName();
			}
		});
		
		int lw  = 100;
		int fw  = 150;
		int gap = 12;
		FlowPanel panel = new FlowPanel();
		panel.getElement().getStyle().setPadding(20, Unit.PX);
		
		RelativeColumn column = new RelativeColumn();
		column.setRowSpacing(20);
		RelativeLayout layout = RelativeLayout.create(fw, LabelAlignment.Top, lw);
		
		column.addRow()
			  .add(new SimpleLabel("金额").setBold(true), amountField, layout)
			  .addSpace(gap)
			  .add(new SimpleLabel("货币").setBold(true), currencyField, layout)
			  .addSpace(gap)
			  .add(new SimpleLabel("类型").setBold(true), typeField, layout)
			  .addSpace(gap)
			  .add(new SimpleLabel("状态").setBold(true), statusField, layout)
			  ;
		
		column.addRow()
		  	  .add(new SimpleLabel("描述").setBold(true), descriptionField, RelativeLayout.create(fw * 3, LabelAlignment.Top, lw));
		
		panel.add(column.getPanel());
		return panel;
	}

	@Override
	public IsButton[] getButtons() {
		return new IsButton[]{
			   new IsButton("递交",CommonResources.INSTANCE.accept16(), new WidgetListener(){
				   @Override
				   public void valueUpdated(Widget widget) {
					    if (amountField.getDouble() == null || amountField.getDouble() == 0.0){
					    	amountField.markError("请输入花费费用");
					    	return;
					    }
					   
					    if (currencyField.getSelectedOption() == null){
					    	currencyField.markError("请选择货币类型");
					    	return;
					    }
					    
					    if (typeField.getSelectedOption() == null){
					    	typeField.markError("请选择花费类型");
					    	return;
					    }
					    
					    if (statusField.getSelectedOption() == null){
					    	statusField.markError("请选择花费状态");
					    	return;
					    }
					    
						AddCostRequest request = new AddCostRequest();
					    request.setAmount(amountField.getDouble());
					    request.setCurrency(currencyField.getSelectedOption().getName());
					    request.setDescription(descriptionField.getTrimmedValueOrEmptyString());
					    request.setType(typeField.getSelectedOption());
					    request.setStatus(statusField.getSelectedOption().getStatus());
					    
					    String passport = SessionManager.getPassport();
					    Services.CostService.AddCost(passport, request,new AsyncCallback<Boolean>(){
							@Override
							public void onFailure(Throwable caught) {
								ErrorHandler.handleServiceError(caught);
							}

							@Override
							public void onSuccess(Boolean result) {
								if(result == Boolean.TRUE) {
								   action.run();
								   closeDialog();
								} 
							}
					    });
				   }
			   })
		};
	}
	
	@Override
	public void onClosing(WindowHandle windowHandle) {
		windowHandle.close();
	}

}
